"implements a COM version of the command line interface to the simulator"""

#import the COM stuff
import sys, os

from win32com.server.exception import COMException
import win32com.server.register
import win32com.server.util
import win32com.client.dynamic

from sim.cmd import CommandInterface
from pythoncom import CLSCTX_LOCAL_SERVER, CLSCTX_INPROC
from Pfd import PFD

class Sim42PFD(object):
    _reg_clsid_ = "{7515DDFC-3E76-40b4-A46F-DCD337514F9E}"
    _reg_desc_  = "Sim42 PFD engine interface"
    _reg_progid_ = "Sim42PFD.engine"
    _reg_clsctx_ = CLSCTX_INPROC
    _public_methods_ = ['PyInfoMessage', 'Setup']
    _public_attrs_ = []
    _readonly_attrs_ = []
    
    def __init__(self):
        self.test = True
        self.Pfd = None
        if self.test:
            f = open('C:\\junk.txt', 'a')
            f.write('\nCreating Sim42PFD')
            f.close()

    def Setup(self, simcom=None):
        # called by Sim42 immediately after my creation
        if self.test:
            f = open('C:\\junk.txt', 'a')
            f.write('\nSim42PFD.Setup')
            f.close()
        if simcom:
            # store the Sim42 interface pointer
            self.sim42 = win32com.client.Dispatch(simcom)
            # register myself for call backs
            idPFD = win32com.server.util.wrap(self)
            self.sim42.SetCallBackComObject(idPFD)
        else:
            #Create a standalone instance of the CommandInterface and
            #pass myself as the handler for messages
            self.sim42 = TestCLI(infoCallBack=self)
            
        # test creating a heater
        #if self.test:
            #self.TestEval('/h = Heater.Heater()')
        
        #self.Pfd.make1()
        #self.Pfd.make2(0)
        
    def DrawPfd(self):
        self.Pfd = PFD(self.sim42)
        self.Pfd.make()
        self.Pfd.makee()

    def TestEval(self, cmd):
        # some testing code
        f = open('C:\\junk.txt', 'a')
        f.write('\nSim42PFD.TestEval: ' + cmd)
        f.close()        
        return self.sim42.Eval(cmd)    

    def PyInfoMessage(self, message, args):
        if self.test:        
            f = open('C:\\junk.txt', 'a')
            f.write('\nSim42PFD.PyInfoMessage: ' + str(message) + ',' +  str(args))
            f.close()
        self.Pfd.Message(message, args)
        return None

    def handleMessage(self, message, args):
        """most basic of call backs"""
        self.PyInfoMessage(message, args)

class TestCLI(CommandInterface.CommandInterface):
    def Eval(self, cmd):
        #print cmd
        f = open('C:\\junk.txt', 'a')
        f.write('\nSim42PFD.TestEval: ' + cmd)
        f.close()       
        return self.ProcessCommandString(cmd)
 
if __name__ == '__main__':
    import win32com.server.register
    import _winreg
    if len(sys.argv) > 1 and sys.argv[1] == 'unregister':
        win32com.server.register.UnregisterClasses(Sim42PFD)
        software_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE')
        vmg_key = _winreg.OpenKey(software_key, 'VMG')
        _winreg.DeleteKey(vmg_key, 'Sim42PFD')
        _winreg.CloseKey(vmg_key)
        _winreg.CloseKey(software_key)
    else:
        win32com.server.register.UseCommandLine(Sim42PFD)
        software_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE')
        vmg_key = _winreg.CreateKey(software_key, 'VMG')
        _winreg.SetValue(vmg_key, 'Sim42PFD', _winreg.REG_SZ, os.path.abspath(os.curdir))
        _winreg.CloseKey(vmg_key)
        _winreg.CloseKey(software_key)
